import * as http from "@/request/http";
import { reactive, ref, Ref } from "vue";

export function UseTable<P, R extends { list:any, total: number }>(reqAPI: (params: P) => RES<R>, reqParams: P) {
    const tableData: Ref<ListModel[]> = ref([]); //数组,遍历每一条都是对象

    const paginationdata = reactive({
        currentPage: 1,
        pageSize: 5,
        total: 10,
    });
    const adminLists = async () => {
        let res = await reqAPI({
            pageNum: paginationdata.currentPage,
            pageSize: paginationdata.pageSize,
            ...reqParams,
        });
        tableData.value = res.data.list;
        paginationdata.total = res.data.total;
    };
    adminLists();
    // 分页器
    const handleSizeChange = (val: number) => {
        console.log(`${val} items per page`);
        adminLists();
    };
    const handleCurrentChange = (val: number) => {
        console.log(`current page: ${val}`);
        adminLists();
    };

    return {tableData, paginationdata, handleSizeChange, handleCurrentChange, adminLists }
}

